package com.cj.mobile.iabp.mvp.ui.activity;

import static com.jess.arms.utils.Preconditions.checkNotNull;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cj.mobile.iabp.R;
import com.cj.mobile.iabp.app.global.Constant;
import com.cj.mobile.iabp.app.utils.CommonUtils;
import com.cj.mobile.iabp.di.component.DaggerRemoveInfoComponent;
import com.cj.mobile.iabp.mvp.contract.RemoveInfoContract;
import com.cj.mobile.iabp.mvp.model.entity.RemoveInfo;
import com.cj.mobile.iabp.mvp.presenter.RemoveInfoPresenter;
import com.cj.mobile.iabp.mvp.ui.adapter.SignInImgListAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created on 2025/09/30 18:01
 * 查看撤机信息
 *
 * @author Love_xie
 * module name is RemoveInfoActivity
 */
public class RemoveInfoActivity extends BaseActivity<RemoveInfoPresenter> implements RemoveInfoContract.View {
    /*----------------------------------------------------------------控件信息----------------------------------------------------------------*/
    @BindView(R.id.txvi_removeinfo_code)
    TextView txviCode;                                                                              // 登记号
    @BindView(R.id.txvi_removeinfo_name)
    TextView txviName;                                                                              // 患者姓名
    @BindView(R.id.txvi_removeinfo_remove)
    TextView txviRemove;                                                                            // 撤机类型
    @BindView(R.id.txvi_removeinfo_concern)
    TextView txviConcern;                                                                           // 关注
    @BindView(R.id.txvi_removeinfo_removetime)
    TextView txviRemoveTime;                                                                        // 撤机时间
    @BindView(R.id.txvi_removeinfo_runtime)
    TextView txviRunTime;                                                                           // 运行时间
    @BindView(R.id.txvi_removeinfo_state)
    TextView txviState;                                                                             // 撤机时患者状态
    @BindView(R.id.txvi_removeinfo_complication)
    TextView txviComplication;                                                                      // 并发症
    @BindView(R.id.revi_removeinfo_img)
    RecyclerView reviImgList;                                                                       // 特殊或异常情况拍照
    /*----------------------------------------------------------------业务信息----------------------------------------------------------------*/
    // 数据源
    private RemoveInfo info;

    // 患者姓名
    private String name;
    // 登记号
    private String code;

    @Inject
    GridLayoutManager mGridLayoutManager;
    @Inject
    List<String> mRecyImgList;
    @Inject
    SignInImgListAdapter mImgAdapter;

    /**
     * 根据主题使用不同的颜色。
     * 如果想要纯透明，则需要重写此方法，返回值为 -1 即可。
     */
    public int useStatusBarColor() {
        TypedValue typedValue = new TypedValue();
        getTheme().resolveAttribute(android.R.color.white, typedValue, true);
        return typedValue.data;
    }

    /**
     * 将状态栏改为浅色模式(状态栏 icon 和字体会变成深色)
     */
    public boolean useLightStatusBar() {
        return true;
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerRemoveInfoComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_remove_info;
    }

    public void getBundleValues(Bundle bundle) {
        if (bundle != null) {
            info = bundle.getParcelable("info");
            code = bundle.getString("code");
            name = bundle.getString("name");
        }
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        setTitle("撤机信息");

        getBundleValues(getIntent().getExtras());
        initRecyclerView();

        loadData();
    }

    private void initRecyclerView() {
        ArmsUtils.configRecyclerView(reviImgList, mGridLayoutManager);
        reviImgList.setAdapter(mImgAdapter);
        mImgAdapter.setOnItemClickListener((view, viewType, data, position) -> {
            String val = (String) data;
            showImage(val);
        });
    }

    /**
     * 显示图片
     */
    public void showImage(String image) {
        if (!TextUtils.isEmpty(image)) {
            Intent intent = new Intent(this, PhotoViewActivity.class);
            intent.putExtra(Constant.IMAGE_URL, image);
            startActivity(intent);
        }
    }

    private void loadData() {
        if (info != null) {
            txviCode.setText(CommonUtils.isEmptyReturnStr(code, "暂无"));
            txviName.setText(CommonUtils.isEmptyReturnStr(name));
            txviRemove.setText(CommonUtils.isEmptyReturnStr(info.getRemoveType()));
            txviConcern.setText(CommonUtils.isEmptyReturnStr(info.getFocuse()));
            txviRemoveTime.setText(CommonUtils.isEmptyReturnStr(info.getRemoveTime()));
            txviRunTime.setText(CommonUtils.isEmptyReturnStr(info.getRunTimeStr()));
            txviState.setText(CommonUtils.isEmptyReturnStr(info.getIsAlive()));

            if (TextUtils.isEmpty(info.getMoreDiagTxt())) {
                txviComplication.setText(CommonUtils.isEmptyReturnStr(info.getMoreDiag()));
            } else {
                txviComplication.setText(CommonUtils.isEmptyReturnStr(info.getMoreDiagTxt()));
            }

            if (!TextUtils.isEmpty(info.getImages())) {
                String[] path = info.getImages().split(",");
                mRecyImgList.clear();
                Collections.addAll(mRecyImgList, path);
                mImgAdapter.notifyDataSetChanged();
            }

        }
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }
}